#ifdef PM_MANAGER_H_INCLUDED

template<typename O>
bool pm::Manager::get_by_id(db::ID id, O& obj)
{
	try {
		wxString sql = wxString::Format(wxT("select * from %s where id = ?"), O::TABLE_NAME.c_str());
		wxSQLite3Statement stmt = m_db.PrepareStatement(sql);
		stmt.Bind(1, id);
		wxSQLite3ResultSet res = stmt.ExecuteQuery();
		if (res.NextRow() == false) {
			return false;
		}
		obj = O(res);
	}
	catch (wxSQLite3Exception& e) {
		wxLogError(wxT("get_by_id(%d, %s) failed : %s"), id, O::TABLE_NAME.c_str(), e.GetMessage().c_str());
		return false;
	}
	return true;
}

template <typename O>
void pm::Manager::select_all(std::list<O>& objs)
{
	try {
		wxString sql = wxString::Format(wxT("select * from %s"), O::TABLE_NAME.c_str());
		wxSQLite3ResultSet res = m_db.ExecuteQuery(sql);
		while (res.NextRow()) {
			objs.push_back( O(res) );
		}
	}
	catch (wxSQLite3Exception& e) {
		wxLogError(wxT("select_all(%s) failed : %s"), O::TABLE_NAME.c_str(), e.GetMessage().c_str());
	}
}

template <typename O>
bool pm::Manager::remove(db::ID id)
{
	try {
		wxString sql; 
		sql << wxT("delete from ") << O::TABLE_NAME << wxT(" where id = ") << id;
		return m_db.ExecuteUpdate(sql) == 1;
	}
	catch (wxSQLite3Exception& e) {
		wxLogError(wxT("remove(%d, %s) failed : %s"), id, O::TABLE_NAME.c_str(), e.GetMessage().c_str());
	}
	return false;
}

template<typename O>
bool pm::get_by_id(db::ID id, O& obj)
{
	return ManagerST::Get()->get_by_id(id, obj);
}

template <class O>
void pm::select_all(std::list<O>& objs)
{
	return ManagerST::Get()->select_all(objs);
}

template <typename O>
bool pm::remove(db::ID id)
{
	return ManagerST::Get()->remove<O>(id);
}

#endif // PM_MANAGER_H_INCLUDED
